package com.i360day.invoker.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;

import java.util.concurrent.TimeUnit;

/**
 * @author liju.z
 */
@Configuration
@ConfigurationProperties(prefix = "spring.invoker")
public class HttpInvokerProperties {
    /**
     * 连接请求超时时间
     */
    private int connectionRequestTimeout = (int) TimeUnit.SECONDS.toMillis(15);
    /**
     * 连接超时时间
     */
    private int connectTimeout = (int) TimeUnit.SECONDS.toMillis(5);
    /**
     * 读取超时时间
     */
    private int readTimeout = (int) TimeUnit.SECONDS.toMillis(30);
    /**
     * 响应设置
     */
    private HttpInvokerResponseProperties response = new HttpInvokerResponseProperties();
    /**
     * 请求客户端
     */
    private HttpInvokerRequestProperties request = new HttpInvokerRequestProperties();
    /**
     * 服务IP+端口地址（127.0.0.1:8080）
     */
    private HttpInvokerServerProperties server;

    private HttpWebSocketProperties webSocket = new HttpWebSocketProperties();
    /**
     * 默认序列化 json。
     * 1、jdk序列化
     * 2、json序列化
     */
    private String serializable;

    public HttpInvokerProperties() {
    }

    public int getConnectionRequestTimeout() {
        return connectionRequestTimeout;
    }

    public void setConnectionRequestTimeout(int connectionRequestTimeout) {
        this.connectionRequestTimeout = connectionRequestTimeout;
    }

    public int getConnectTimeout() {
        return connectTimeout;
    }

    public void setConnectTimeout(int connectTimeout) {
        this.connectTimeout = connectTimeout;
    }

    public int getReadTimeout() {
        return readTimeout;
    }

    public void setReadTimeout(int readTimeout) {
        this.readTimeout = readTimeout;
    }

    public HttpInvokerRequestProperties getRequest() {
        return request;
    }

    public void setRequest(HttpInvokerRequestProperties request) {
        this.request = request;
    }

    public HttpInvokerResponseProperties getResponse() {
        return response;
    }

    public void setResponse(HttpInvokerResponseProperties response) {
        this.response = response;
    }

    public HttpInvokerServerProperties getServer() {
        return server;
    }

    public void setServer(HttpInvokerServerProperties server) {
        this.server = server;
    }

    public String getSerializable() {
        return serializable;
    }

    public void setSerializable(String serializable) {
        this.serializable = serializable;
    }

    public HttpWebSocketProperties getWebSocket() {
        return webSocket;
    }

    public void setWebSocket(HttpWebSocketProperties webSocket) {
        this.webSocket = webSocket;
    }

    /**
     * 请求
     */
    public static class HttpInvokerRequestProperties {
        /**
         * 请求客户端
         * 1、httpClient
         * 2、okhttp
         */
        private String client = "httpClient";
        /**
         * 最大链接树
         */
        private int maxConnections = 100;
        /**
         * 最大空闲链接，单位毫秒
         */
        private int timeToLive = (int) TimeUnit.SECONDS.toMillis(30);

        public String getClient() {
            return client;
        }

        public void setClient(String client) {
            this.client = client;
        }

        public int getMaxConnections() {
            return maxConnections;
        }

        public void setMaxConnections(int maxConnections) {
            this.maxConnections = maxConnections;
        }

        public int getTimeToLive() {
            return timeToLive;
        }

        public void setTimeToLive(int timeToLive) {
            this.timeToLive = timeToLive;
        }
    }

    /**
     * 响应
     */
    public static class HttpInvokerResponseProperties {
        /**
         * rpc响应格式
         */
        private MediaType contentType = MediaType.APPLICATION_JSON;

        public MediaType getContentType() {
            return contentType;
        }

        public void setContentType(MediaType contentType) {
            this.contentType = contentType;
        }


        public void setContentType(String contentType) {
            this.contentType = MediaType.valueOf(contentType);
        }
    }

    /**
     * 服务端
     */
    public static class HttpInvokerServerProperties {
        /**
         * 服务IP+端口地址（127.0.0.1:8080）
         */
        private String address;

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }
    }

    public static class HttpWebSocketProperties {
        private String endpoint = "httpInvoker";

        public String getEndpoint() {
            return endpoint.startsWith("/") ? endpoint : "/" + endpoint;
        }

        public void setEndpoint(String endpoint) {
            this.endpoint = endpoint;
        }
    }
}
